package com.memo4android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.memo4android.model.AppModel;
import com.memo4android.model.Dossier;
import com.memo4android.model.ItemTodoList;

public class ModifyTodoList extends Activity implements OnClickListener{
	private ItemTodoList tdl;
	private AppModel model;
	private EditText edit; 
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.frame_modify_todolist);
        
        model = AppModel.getInstance(this);
        String TodoListName = getIntent().getStringExtra("TodoListName");
        
        setTitle(this.getTitle() + " - " + TodoListName);
        
        if (model.getTodoListFaible(TodoListName)!=null)
		{
			tdl = model.getTodoListFaible(TodoListName);
		}
		else if (model.getTodoListMoyennne(TodoListName)!=null)
		{
			tdl = model.getTodoListMoyennne(TodoListName);
		} 
		else if (model.getTodoListForte(TodoListName)!=null)
		{
			tdl = model.getTodoListForte(TodoListName);
		}
        edit = (EditText) findViewById(R.id.titleModifyTodoList);
        edit.setText(TodoListName);
        
        ((Button)this.findViewById(R.id.buttonCancelModifyTodoList)).setOnClickListener(this);
        ((Button)this.findViewById(R.id.buttonDeleteModifyTodoList)).setOnClickListener(this);
        ((Button)this.findViewById(R.id.buttonValidModifyTodoList)).setOnClickListener(this);
	}
	
    @Override
    public void onBackPressed(){
		Intent intent_allnotes = new Intent(this ,MainView.class);
		intent_allnotes.putExtra("label", "all notes");
		this.startActivity(intent_allnotes);    	
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		
		case R.id.buttonValidModifyTodoList:
			
	        if(tdl != null){ 
		        tdl.setTitre(edit.getText().toString());
		        model.saveTodoListToBDD(tdl);
	        }
			
			this.finish();
			break;
			
		case R.id.buttonCancelModifyTodoList:
			this.finish();
			break;
			
		case R.id.buttonDeleteModifyTodoList:
			
	        if (tdl.getPriorite()=="forte")
			{
	        	model.getForteTodoLists().remove(tdl);
			}
			else if (tdl.getPriorite()=="faible")
			{
	        	model.getFaibleTodoLists().remove(tdl);
			}
			else if (tdl.getPriorite()=="moyenne")
			{
	        	model.getMoyenneTodoLists().remove(tdl);
			}
			model.deleteTodoListFromBDD(tdl);
			Intent intent_alltodos = new Intent(this ,ToDoListsActivity.class);
			this.startActivity(intent_alltodos);
			break;
		
		}
		
	}

}
